<template>
  <div class="root" style="background-color: white; width: 100%; height: 100%;">
      <el-row style="background-color: white;">
          <el-col style="margin: 0px 5%; width: 90%;">
            <p style="text-align: center; font-size: 30px;">高校党委理论学习中心组</p>
            <el-form :model="form" :rules="rules" ref="form" style="font-weight: 600;">
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="学校">
                          <el-input disabled style="width: 70%" v-model="form.school"></el-input>
                          <!-- <el-select style="width: 80%" v-model="form.school" placeholder="选择或输入学校" clearable filterable>
                            <el-option v-for="item in schools" :key="item.sch_id" :label="item.sch_name" :value="item.sch_name"></el-option>
                          </el-select> -->
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="学习时间" prop="datetime">
                          <el-date-picker
                            v-model="form.datetime"
                            unlink-panels
                            type="daterange"
                            style="width: 70%"
                            range-separator=" 至 "
                            start-placeholder="开始日期"
                            end-placeholder="结束日期">
                          </el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                      <el-form-item label="学习次数" required>
                        <el-form-item prop="learn_year" style="display: inline-block">
                          <el-input
                              v-model="form.learn_year"
                              class="xiahuaxian"
                            >
                            </el-input>
                            年度第
                        </el-form-item>
                        <el-form-item prop="learn_num" style="display: inline-block">
                          <el-input
                              v-model="form.learn_num"
                              class="xiahuaxian"
                            >
                            </el-input>
                            次中心组学习
                        </el-form-item>
                      </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item label="学习内容" prop="textcontent">
                            <el-input
                              type="textarea"
                              placeholder="请输入内容(500字以内)"
                              v-model="form.textcontent"
                              :maxlength="500"
                              show-word-limit
                              style="width: 85%"
                            >
                            </el-input>
                        </el-form-item>
                    </el-col>
                    <el-col>
                        <el-form-item label="学习形式（多选）" prop="studyway">
                            <el-checkbox-group v-model="form.studyway" @change="changeOther1">
                              <el-checkbox :label="0">集体学习研讨</el-checkbox>
                              <el-checkbox :label="1">个人自学</el-checkbox>
                              <el-checkbox :label="2">专题调研</el-checkbox>
                              <el-checkbox :label="3">扩大会</el-checkbox>
                              <el-checkbox :label="4">其他</el-checkbox>
                              <el-form-item prop="other1" style="display: inline-block">
                                <el-input :disabled="disabled1" class="xiahuaxian" v-model="form.other1"></el-input>
                              </el-form-item>
                            </el-checkbox-group>
                        </el-form-item>
                    </el-col>
                    <el-col>
                        <el-form-item label="参加人员（多选）" prop="joinpeople">
                          <el-checkbox-group v-model="form.joinpeople" @change="changeOther2">
                            <el-checkbox :label="0">校党委中心组成员</el-checkbox>
                            <el-checkbox :label="1">全体校领导</el-checkbox>
                            <el-checkbox :label="2">全体中层以上党员干部</el-checkbox>
                            <el-checkbox :label="3">各单位/部门主要负责人</el-checkbox>
                            <el-checkbox :label="4">副处级及以上干部</el-checkbox>
                            <el-checkbox :label="5">二级党组织书记</el-checkbox>
                            <el-checkbox :label="6">其他</el-checkbox>
                            <el-form-item prop="other2" style="display: inline-block">
                              <el-input :disabled="disabled2" class="xiahuaxian" v-model="form.other2"></el-input>
                            </el-form-item>
                          </el-checkbox-group>
                        </el-form-item>
                    </el-col>
                    <el-col>
                        <el-form-item label="相关新闻链接" prop="news">
                          <el-input v-model="form.news" placeholder="非必填项超链接" style="width: 85%;"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col>
                        <p style="color: #606266; font-size: 16px;">报告人非校党委中心组成员的请填写：</p>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item label="报告人" prop="speaker">
                            <el-input
                              type="textarea"
                              placeholder="请输入报告人(纯文本20字以内)"
                              v-model="form.speaker"
                              :maxlength="20"
                              show-word-limit
                              style="width: 85%"
                            >
                            </el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item label="报告人职务/职称" prop="job">
                            <el-input
                              type="textarea"
                              placeholder="请输入报告人职务/职称(纯文本30字以内)"
                              v-model="form.job"
                              :maxlength="30"
                              show-word-limit
                              style="width: 85%"
                            >
                            </el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item label="备注" prop="remark">
                            <el-input
                              type="textarea"
                              placeholder="请输入备注"
                              v-model="form.remark"
                              :maxlength="100"
                              show-word-limit
                              style="width: 85%"
                            >
                            </el-input>
                        </el-form-item>
                    </el-col>
                    <el-col>
                        <p style="color: #606266; font-size: 16px;"> <span style="color: red;">*</span> 添加附件（限一个PDF，必填项）：</p>
                    </el-col>
                    <el-col :span="6" class="Label">
                        <el-form-item label="学习方案" prop="studyPlan">
                           <el-upload
                             class="upload-demo"
                             ref="upload2"
                             name="file"
                             :limit="1"
                             :data="{ sch_name: form.school, sch_id: form.sch_id }"
                             :file-list="fileList2"
                             :on-success="handleSuccess2"
                             :on-exceed="handleExceed"
                             :on-change="handleChange1"
                             :on-remove="handleRemove1"
                             action="http://8.142.34.26:8096/php/index.php/admin/Upload/upload2"
                             accept='.PDF'>
                             <el-button size="small">
                                <i class="el-icon-upload"></i>
                                <span>点击上传</span>
                             </el-button>
                             <!-- <div class="el-upload__tip" slot="tip">只能上传pdf文件</div> -->
                           </el-upload>
                        </el-form-item>
                    </el-col>
                    <el-col :span="6" class="Label">
                        <el-form-item label="学习记录" prop="studyRecord">
                           <el-upload
                             class="upload-demo"
                             ref="upload3"
                             name="file"
                             :limit="1"
                             :data="{ sch_name: form.school, sch_id: form.sch_id}"
                             :file-list="fileList3"
                             :on-success="handleSuccess3"
                             :on-exceed="handleExceed"
                             :on-change="handleChange2"
                             :on-remove="handleRemove2"
                             action="http://8.142.34.26:8096/php/index.php/admin/Upload/upload3"
                             accept='.PDF'>
                             <el-button size="small">
                                <i class="el-icon-upload"></i>
                                <span>点击上传</span>
                             </el-button>
                             <!-- <div class="el-upload__tip" slot="tip">只能上传pdf文件</div> -->
                           </el-upload>
                        </el-form-item>
                    </el-col>
                    <el-col :span="6" class="Label">
                        <el-form-item label="学习报告" prop="studyReport">
                           <el-upload
                             class="upload-demo"
                             ref="upload1"
                             name="file"
                             :limit="1"
                             :data="{ sch_name: form.school, sch_id: form.sch_id }"
                             :file-list="fileList1"
                             :on-success="handleSuccess1"
                             :on-exceed="handleExceed"
                             :on-change="handleChange3"
                             :on-remove="handleRemove3"
                             action="http://8.142.34.26:8096/php/index.php/admin/Upload/upload1"
                             accept='.PDF'
                             >
                             <el-button size="small">
                                <i class="el-icon-upload"></i>
                                <span>点击上传</span>
                             </el-button>
                           </el-upload>
                        </el-form-item>
                    </el-col>
                    <el-col :span="6" class="Label">
                        <el-form-item label="其他材料" prop="otherFile">
                           <el-upload
                             class="upload-demo"
                             ref="upload4"
                             name="file"
                             :limit="1"
                             :data="{ sch_name: form.school, sch_id: form.sch_id }"
                             :file-list="fileList4"
                             :on-success="handleSuccess4"
                             :on-exceed="handleExceed"
                             :on-change="handleChange4"
                             :on-remove="handleRemove4"
                             action="http://8.142.34.26:8096/php/index.php/admin/Upload/upload4"
                             accept='.PDF'
                             >
                             <el-button size="small">
                                <i class="el-icon-upload"></i>
                                <span>点击上传</span>
                             </el-button>
                           </el-upload>
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                      <el-form-item style="text-align: center; margin: 20px 0px;">
                          <el-button type="primary" @click="submitForm('form', 'upload1', 'upload2', 'upload3', 'upload4')">提交</el-button>
                          <el-button type="warning" @click="resetForm('form', 'upload1', 'upload2', 'upload3', 'upload4')">重置</el-button>
                          <!-- <el-button>管理员登录</el-button> -->
                      </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
          </el-col>
      </el-row>
  </div>
</template>

<script>
import moment from 'moment'
import http from '../../../assets/js/http'
export default {
  data () {
    return {
      form: {
        studyway: [],
        school: '',
        sch_id: '',
        datetime: '',
        textcontent: '',
        joinpeople: [],
        news: '',
        speaker: '',
        job: '',
        remark: '',
        file_id1: '',
        file_id2: '',
        file_id3: '',
        file_id4: '',
        learn_year: '',
        learn_num: ''
      },
      disabled1: true,
      disabled2: true,
      userInfo: Lockr.get('userInfo'),
      fileList1: [],
      fileList2: [],
      fileList3: [],
      fileList4: [],
      mode: {},
      schools: [],
      value: '',
      rules: {
        datetime: [
          { required: true, message: '请选择学习时间', trigger: 'blur' }
        ],
        learn_year: [
          { required: true, message: '请选择学习年度', trigger: 'blur' }
        ],
        learn_num: [
          { required: true, message: '请选择学习次数', trigger: 'blur' }
        ],
        textcontent: [
          { required: true, message: '请选择学习内容', trigger: 'blur' }
        ],
        studyway: [
          { required: true, message: '请选择学习形式', trigger: 'blur' }
        ],
        joinpeople: [
          { required: true, message: '请选择学习参加人员', trigger: 'blur' }
        ],
        // speaker: [
        //   { required: true, message: '请选择学习报告人', trigger: 'blur' }
        // ],
        // job: [
        //   { required: true, message: '请选择学习报告人职称', trigger: 'blur' }
        // ],
        studyPlan: [
          { required: true, message: '请选择学习方案', trigger: 'blur' }
        ],
        studyRecord: [
          { required: true, message: '请选择学习记录', trigger: 'blur' }
        ],
        studyReport: [
          { required: true, message: '请选择学习报告', trigger: 'blur' }
        ]
        // otherFile: [
        //   { required: true, message: '请选择其他材料', trigger: 'blur' }
        // ]
      }
    }
  },
  activated () {},
  watch: {},
  created () {
    this.init()
  },
  mounted () {},
  methods: {
    init () {
      this.getSchoolsInfo()
    },
    getSchoolsInfo () {
      this.apiPost('admin/Info/getSchByCid').then((res) => {
        this.handelResponse(res, (data) => {
          this.form.school = res.data.sch_name
          this.form.sch_id = res.data.sch_id
          console.log(this.form)
          // this.schools = res.data
        })
      })
    },
    resetForm (form, upload1, upload2, upload3, upload4) {
    //   console.log(this)
      this.$refs[form].resetFields()
      this.$refs[upload1].clearFiles()
      this.$refs[upload2].clearFiles()
      this.$refs[upload3].clearFiles()
      this.$refs[upload4].clearFiles()
    //   this.$refs[form].clearFiles()
    },
    handleExceed() {
      _g.toastMsg('warning', '只能上传一个附件')
    },
    handleChange1(file, fileList) {
      this.rules.studyPlan = []
      this.$refs.form.clearValidate('studyPlan')
      // console.log(this.$refs.upload1)
    },
    handleRemove1(file, fileList) {
      this.rules.studyPlan = [
        { required: true, message: '请选择学习方案', trigger: 'blur' }
      ]
    },
    handleChange2(file, fileList) {
      this.rules.studyRecord = []
      this.$refs.form.clearValidate('studyRecord')
      // console.log(this.$refs.upload1)
    },
    handleRemove2(file, fileList) {
      this.rules.studyRecord = [
        { required: true, message: '请选择学习记录', trigger: 'blur' }
      ]
    },
    handleChange3(file, fileList) {
      this.rules.studyReport = []
      this.$refs.form.clearValidate('studyReport')
      // console.log(this.$refs.upload1)
    },
    handleRemove3(file, fileList) {
      this.rules.studyReport = [
        { required: true, message: '请选择学习报告', trigger: 'blur' }
      ]
    },
    // handleChange4(file, fileList) {
    //   this.rules.otherFile = []
    //   this.$refs.form.clearValidate('otherFile')
    //   // console.log(this.$refs.upload1)
    // },
    // handleRemove4(file, fileList) {
    //   this.rules.otherFile = [
    //     { required: true, message: '请选择其他材料', trigger: 'blur' }
    //   ]
    // },
    handleSuccess1 (response, file, filelist) {
      console.log(this.fileList1)
      if (response.code === 200) {
        this.form.file_id1 = response.data.learn_baogao_file_id
        console.log(this.form.file_id1)
      } else {
        this.$message({
          type: 'warning',
          message: '提交失败!'
        })
      }
    },
    handleSuccess2 (response, file, filelist) {
      if (response.code === 200) {
        this.form.file_id2 = response.data.learn_fangan_file_id
        console.log(this.form.file_id2)
      } else {
        this.$message({
          type: 'warning',
          message: '提交失败!'
        })
      }
    },
    handleSuccess3 (response, file, filelist) {
      console.log(file)
      if (response.code === 200) {
        this.form.file_id3 = response.data.learn_jilu_file_id
        console.log(this.form.file_id3)
      } else {
        this.$message({
          type: 'warning',
          message: '提交失败!'
        })
      }
    },
    handleSuccess4 (response, file, filelist) {
      console.log(file)
      if (response.code === 200) {
        this.form.file_id4 = response.data.learn_other_file_id
        console.log(this.form.file_id4)
      } else {
        this.$message({
          type: 'warning',
          message: '提交失败!'
        })
      }
    },
    changeOther1 (val) {
      console.log(val)
      if (val.length !== 0) {
        for (var i = 0; i < val.length; i++) {
          if (val[i] === 4) {
            this.disabled1 = false
            return
          } else {
            this.disabled1 = true
          }
        }
      } else {
        this.disabled1 = true
      }
    },
    changeOther2 (val) {
      console.log(val)
      if (val.length !== 0) {
        for (var i = 0; i < val.length; i++) {
          if (val[i] === 6) {
            this.disabled2 = false
            return
          } else {
            this.disabled2 = true
          }
        }
      } else {
        this.disabled2 = true
      }
    },
    submitForm (form, upload1, upload2, upload3, upload4) {
      if (this.form.datetime.length !== 0) {
        for (var i = 0; i < 2; i++) {
          this.form.datetime[i] = moment(this.form.datetime[i]).format('YYYY-MM-DD')
        }
      }
      // for (var j = 0; j < this.form.studyway.length; j++) {
      //   if (this.form.studyway[j] === 4) return
      //   this.form.other1 = ''
      // }
      // for (var k = 0; k < this.form.joinpeople.length; k++) {
      //   if (this.form.joinpeople[k] !== 6) {
      //     this.form.other2 = ''
      //   }
      // }
      // this.form.datetime = moment(this.form.datetime).format('YYYY-MM-DD HH:mm:ss')
      console.log(this.form)
      this.$refs[form].validate((valid) => {
        if (valid) {
          this.$confirm('是否确认提交？', '核对信息', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
            center: true
          }).then(() => {
            this.apiPost('admin/Info/InfoAdd', this.form).then((res) => {
              this.handelResponse(res, (data) => {
                this.$message({
                  type: 'success',
                  message: '提交成功!'
                })
                this.$router.push({ name: 'consultsList' })
              })
            }).catch(() => {
              this.$message({
                type: 'warning',
                message: '提交失败!'
              })
            })
          }).catch(() => {
            this.$message({
              type: 'info',
              message: '已取消提交'
            })
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  },
  mixins: [http]
}
</script>

<style>
.el-form-item__label {
    width: 137px;
    line-height: 50px;
}
.Label .el-form-item .el-form-item__label {
    width: 115px;
}
.xiahuaxian {
  width: 100px;
  display: inline-block;
}
.xiahuaxian .el-input__inner {
  width: 100px;
  margin-left: 5px;
  border: 0;
  border-bottom: 1px solid #888;
  border-radius: 0;
}
/* // .el-upload-dragger {
//     width: 310px !important;
// } */
</style>
